﻿using System.Data;
using DevExpress.XtraEditors;
using System.Windows.Forms;
using System;

namespace Mota.XMLControls
{
    /// <summary>
    /// Represent TextBox based on DX TextEdit
    /// </summary>
    public class DXGuiTextBox:GuiTextBox
    {
        /// <summary>
        /// Create TextEdit control
        /// </summary>
        public DXGuiTextBox()
        {
            visualControl = new TextEdit();
            (visualControl as TextEdit).EditValueChanged += DXGuiTextBox_EditValueChanged;
        }

        void DXGuiTextBox_EditValueChanged(object sender, EventArgs e)
        {
            this.ChangeEventCall(null);
        }

        /// <summary>
        /// GET|SET TextEdit Text property
        /// </summary>
        public override string Text
        {
            get
            {
                return (visualControl as TextEdit).Text;
            }
            set
            {
                (visualControl as TextEdit).Text = value;
            }
        }
        /// <summary>
        /// Add DataBinding to TextEdit Text property
        /// </summary>
        /// <param name="valueBinding">main binding source with current record values</param>
        public override void SetValue(object valueBinding)
        {
            if(valueBinding is BindingSource)
                (visualControl as TextEdit).DataBindings.Add("EditValue", valueBinding as BindingSource, XmlAttributes["XMLAttribute"], true);
        }
        /// <summary>
        /// Dummy object
        /// </summary>
        public override object Binding
        {
            get
            {
                return null;
            }
            set
            {
            }
        }
        public override bool IsValid
        {
            get
            {
                return Required ? (!String.IsNullOrEmpty((visualControl as TextEdit).EditValue as String)):true;
            }
        }
        public override Char PasswordChar
        {
            get
            {
                return (visualControl as TextEdit).Properties.PasswordChar;
            }
            set
            {
                (visualControl as TextEdit).Properties.PasswordChar = value;
            }
        }

        public override int MaxLength
        {
            get
            {
                return (this.visualControl as TextEdit).Properties.MaxLength;
            }
            set
            {
                (this.visualControl as TextEdit).Properties.MaxLength = value;
            }
        }



        public override object GetValue()
        {
            return (visualControl as TextEdit).EditValue;
        }
    }
}
